package com.zwh.minio;

import com.google.common.collect.Multimap;
import io.minio.CreateMultipartUploadResponse;
import io.minio.ListPartsResponse;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.errors.*;
import io.minio.messages.Part;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 扩展分片上传
 *
 * @author doubleZhang
 * @date 2024-10-28 15:45
 */
public class CustomMinioClient extends MinioClient {

    protected CustomMinioClient(MinioClient client) {
        super(client);
    }

    public String initMultiPartUpload(String bucket, String region, String object, Multimap<String, String> headers,
        Multimap<String, String> extraQueryParams)
        throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, ServerException,
        InternalException, XmlParserException, InvalidResponseException, ErrorResponseException {
        CreateMultipartUploadResponse response =
            this.createMultipartUpload(bucket, region, object, headers, extraQueryParams);

        return response.result().uploadId();
    }

    public ObjectWriteResponse mergeMultipartUpload(String bucketName, String region, String objectName,
        String uploadId, Part[] parts, Multimap<String, String> extraHeaders, Multimap<String, String> extraQueryParams)
        throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, ServerException,
        InternalException, XmlParserException, InvalidResponseException, ErrorResponseException {

        return this.completeMultipartUpload(bucketName, region, objectName, uploadId, parts, extraHeaders,
            extraQueryParams);
    }

    public ListPartsResponse listMultipart(String bucketName, String region, String objectName, Integer maxParts,
        Integer partNumberMarker, String uploadId, Multimap<String, String> extraHeaders,
        Multimap<String, String> extraQueryParams)
        throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, ServerException,
        XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
        return this.listParts(bucketName, region, objectName, maxParts, partNumberMarker, uploadId, extraHeaders,
            extraQueryParams);
    }
}
